package cn.htsyb.synergy.security.web.dao;

import cn.htsyb.synergy.security.web.constant.CacheKeyConst;
import cn.htsyb.synergy.security.web.model.SecurityAuthority;
import cn.htsyb.synergy.security.web.model.SecurityUserDetail;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Query;
import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;

/**
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @date 2022/5/24
 * @since 1.0
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class SecurityAuthMetaDao {


    @Resource
    @PersistenceContext
    protected EntityManager entityManager;
    @Cacheable(condition = "#result != null", value = CacheKeyConst.CACHE_AUTH_VISIT_URL_KEY, key = "#p0")
    public Collection<ConfigAttribute> findAttributeByVisitUrl(@NotNull String visitUrl) {
        Query query = entityManager.createQuery("select new cn.htsyb.synergy.security.web.model.SecurityAuthority(sp) from SecurityPermission sp where sp.visitUrl=:visitUrl");
        query.setParameter("visitUrl", visitUrl);
        return query.getResultList();
    }

    @Cacheable(condition = "#result != null", value = CacheKeyConst.CACHE_AUTH_USER_PERMISSION_KEY, key = "#p0")
    public List<SecurityAuthority> findAuthorityByAccountId(@NotNull String userId) {
        Query query = entityManager.createQuery("select new cn.htsyb.synergy.security.web.model.SecurityAuthority(sp) from SecurityUser as su join su.roles as sro join sro.permissions as sp where su.id=:userId order by sp.parentId, sp.sortNum");
        query.setParameter("userId", userId);
        return query.getResultList();
    }

    @Cacheable(condition = "#result != null", value = CacheKeyConst.CACHE_AUTH_ALL_PERMISSION_KEY)
    public Collection<ConfigAttribute> findAttributeAll() {
        Query query = entityManager.createQuery("select new cn.htsyb.synergy.security.web.model.SecurityAuthority(sp) from SecurityPermission sp ");
        return query.getResultList();
    }

    @Cacheable(condition = "#result != null", value = CacheKeyConst.CACHE_AUTH_CONFIG_PERMISSION_KEY)
    public List<SecurityAuthority> findAuthorityAll() {
        Query query = entityManager.createQuery("select new cn.htsyb.synergy.security.web.model.SecurityAuthority(sp) from SecurityPermission sp ");
        return query.getResultList();
    }

    @Cacheable(condition = "#result != null", value = CacheKeyConst.CACHE_AUTH_USER_DETAIL_KEY, key = "#p0")
    public SecurityUserDetail findUserDetailByUsername(@NotNull String username) {
        Query query = entityManager.createQuery("select new cn.htsyb.synergy.security.web.model.SecurityUserDetail(su.id,su.username,su.password,su.expiredTime,su.validStatus) from SecurityUser su where su.username=:username");
        query.setParameter("username", username);
        return (SecurityUserDetail) query.getSingleResult();
    }
}
